<?php namespace Phpcmf\Controllers\Admin;

/**
 * 二次开发时可以修改本文件，不影响升级覆盖
 */

class Bisai extends \Phpcmf\Admin\Mform
{

    public function index() {
        //$this->_Admin_List();
		$tid = dr_safe_filename($_GET['tid']);
		if (strlen($tid)) {
		    $tid = intval($tid);
		    $this->init['where_list'].= ' and tid='.$tid;
		}
		
		$this->init['order_by'] = 'displayorder ASC,inputtime ASC';
		
		$p = [
		    'cid' =>  $this->cid,
		    'tid' =>  $tid,
		];
		
		$this->_List($p);
		
		//dr_debug('_app_clink', $this->_app_clink('mform'));
		
		//$stype = \Phpcmf\Service::M('content', 'zhishi')->son_type($this->index['son_type']);
		
		\Phpcmf\Service::V()->assign([
		    'p' => $p,
		    'stype' => $stype,
		    'clink' => $this->_app_clink('mform'),
		    'cbottom' => $this->_app_cbottom('mform'),
		    'sname' => strlen($tid) ? $stype[$tid] : '全部',
		    'menu' => \Phpcmf\Service::V()->get_value('menu').'<li> <a  href="javascript:dr_help(\'628\');"><i class="fa fa-question-circle"></i> '.dr_lang('在线帮助').'</a> <i class="fa fa-circle"></i> </li>',
		]);
		\Phpcmf\Service::V()->display('bisai_list.html');
    }

    public function add() {
        $this->_Admin_Add();
    }

    public function edit() {
        $this->_Admin_Edit();
    }

    public function show_index() {
        $this->_Admin_Show();
    }

    public function order_edit() {
        $this->_Admin_Order();
    }

    public function del() {
        $this->_Admin_Del();
    }
	/**
	 * 回调处理结果
	 * $data
	 * */
	protected function _Call_Post($data) {
		//dr_debug('data', $data);
		$rt = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_bisai')->where('tid', $data[1]['tid'])->countAllResults();
		//$title = \Phpcmf\Service::M()->db->table('1_'.APP_DIR.'_form_son')->where('cid', $data[1]['cid'])->get()->getRowArray();
		\Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_son')->where('id', $data[1]['tid'])->where('cid', $data[1]['cid'])->update([
		    'bisaishuliang' => $rt,
		]);
		\Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR)->where('id', $data[1]['tid'])->update([
		    'bisai_total' => $rt,
		]);
		//dr_debug('data', 'tid-'.$data[1]['tid'].'cid-'.$data[1]['cid'].'t-'.$rt);
		if ($data[1]['status'] == 9) {
		    $html = $list = '';
		    $cat = dr_cat_value($this->module['mid'], $data[1]['catid']);
		    if ($cat['setting']['html']) {
		        // 生成权限文件
		        if (!dr_html_auth(1)) {
		            $this->_json(0, dr_lang('/cache/html/ 无法写入文件'));
		        }
		        $html = dr_web_prefix('index.php?s='.MOD_DIR.'&c=html&m=showfile&id='.$data[1]['id']);
		        $list = dr_web_prefix('index.php?s='.MOD_DIR.'&c=html&m=categoryfile&id='.$data[1]['catid']);
		    }
		    $this->_json(1, dr_lang('操作成功'), [
		        'id' => $data[1]['id'],
		        'url' => isset($_GET['is_self']) ? dr_url(MOD_DIR.'/home/edit', ['id' => $data[1]['id']]) : '',
		        'catid' => $data[1]['catid'],
		        'htmlfile' => $html,
		        'htmllist' => $list
		    ]);
		} else {
		    if (intval(\Phpcmf\Service::L('input')->post('is_draft'))) {
		        // 草稿
		        $this->_json(1, dr_lang('保存草稿成功'));
		    } elseif ($this->is_post_user) {
		        // 投稿者
		        $this->_json(1, dr_lang('操作成功，等待管理员审核'), [
		            'url' => dr_url($this->_is_admin_auth(MOD_DIR.'/verify/index') ? MOD_DIR.'/verify/index' : MOD_DIR.'/home/index'),
		            'id' => $data[1]['id'],
		            'catid' => $data[1]['catid'],
		        ]);
		    }
		    $this->_json(1, dr_lang('操作成功'), [
		        'id' => $data[1]['id'],
		        'catid' => $data[1]['catid']
		    ]);
		}
	}
}
